package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.User;

import dto.DataManager;

/**
 * Servlet implementation class Payment
 */
@WebServlet("/Payment")
public class Payment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
  
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  request.setCharacterEncoding("UTF-8");  
	       response.setContentType("text/html;charset=UTF-8");
	       
	       DataManager dataManager = new DataManager();
	       int userId = Integer.parseInt(request.getParameter("userId"));
	       int tongtien = Integer.parseInt(request.getParameter("tongtien"));
	       User user = dataManager.getUserWithUserId(userId);
	       dataManager.updateMoney(userId, user.getOwnMoney()-tongtien);
	       dataManager.deleteAllCartByUserId(userId);
	      // out.print("<script type=\"text/javascript\">	alert(\"Đã Thanh Toán thành công !!!\")	</script>");
	       response.sendRedirect("default.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

}
